/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.service;

import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.storage.dao.ActivityDao;
import com.jeesite.modules.storage.dao.BrandDao;
import com.jeesite.modules.storage.dao.GoodsDao;
import com.jeesite.modules.storage.entity.Brand;
import com.jeesite.modules.storage.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 品牌Service
 */
@Service
@Transactional(readOnly=true)
public class BrandService extends CrudService<BrandDao, Brand> {

	@Autowired
	private GoodsDao goodsDao;

	@Autowired
	private ActivityDao activityDao;

	@Autowired
	private ActivityService activityService;

	@Autowired
	private TransActivityService transActivityService;

	/**
	 * 获取单条数据
	 * @param brand
	 * @return
	 */
	@Override
	public Brand get(Brand brand) {
		return super.get(brand);
	}
	
	/**
	 * 查询分页数据
	 * @param brand 查询条件
	 * @param brand.page 分页对象
	 * @return
	 */
	@Override
	public Page<Brand> findPage(Brand brand) {
		Page<Brand> brandPage = super.findPage(brand);
		for (Brand item : brandPage.getList()){
			item.setActivityNum(activityDao.findCountByBrandIdOrCateId(item.getId(),null));
		}
		return brandPage;
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param brand
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(Brand brand) {
		brand.setGoodsNum(brand.getIsNewRecord() ? 0 :goodsDao.findCountByBrandIdOrCateId(brand.getId(),null));
//		if(StringUtils.isNotBlank(brand.getLogo())){
//			String pic = "/userfiles/brand/logo/" + UUID.randomUUID() + "." + WebUtil.getExtension(brand.getLogo());
//			WebUtil.thumbnails(WebUtil.getRootPath(brand.getLogo()),WebUtil.getRootPath(pic));
//			brand.setLogo(pic);
//		}
//		if(StringUtils.isNotBlank(brand.getTransparentLogo())){
//			String pic = "/userfiles/brand/transparentlogo/" + UUID.randomUUID() + "." + WebUtil.getExtension(brand.getTransparentLogo());
//			WebUtil.thumbnails(WebUtil.getRootPath(brand.getTransparentLogo()),WebUtil.getRootPath(pic));
//			brand.setTransparentLogo(pic);
//		}
//		if(StringUtils.isNotBlank(brand.getBackground())){
//			String pic = "/userfiles/brand/background/" + UUID.randomUUID() + "." + WebUtil.getExtension(brand.getBackground());
//			WebUtil.thumbnails(WebUtil.getRootPath(brand.getBackground()),WebUtil.getRootPath(pic));
//			brand.setBackground(pic);
//		}

		super.save(brand);
	}
	
	/**
	 * 更新状态
	 * @param brand
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(Brand brand) {
		super.updateStatus(brand);
	}
	
	/**
	 * 删除数据
	 * @param brand
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(Brand brand) {
		super.delete(brand);
	}

	/*
        获取所有品牌
    */
	public List<Brand> getAllList(){
		return super.dao.findAllList();
	}

	/**
	 * 查找Id【名称】
	 * @param name 名称
	 * @return 查找Id
	 */
	public String getIdByName(String name){
		return dao.findIdByName(name);
	}

	/**
	 * 获取仓库【Id】
	 * @param brandId 品牌Id
	 * @return 仓库
	 */
	public	String getStoreById(String brandId){
		return dao.findStoreById(brandId);
	}

	/**
	 * 获取品牌店铺
	 * @param transMemberId 转发会员Id
	 * @param memberId
	 * @param activityId
	 * @param transActivityId 转发活动Id
	 * @return 品牌店铺
	 */
	public Map getContent(String transMemberId, String memberId, String activityId,String transActivityId) {
		if(StringUtils.isBlank(activityId) && StringUtils.isNotBlank(transActivityId)){
			activityId = transActivityService.getActivityIdById(transActivityId);
		}
		return activityService.getBrand(transMemberId,memberId,activityId);
	}
}